﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Electronic_work_order.Model;
using Microsoft.EntityFrameworkCore;

namespace Electronic_work_order.Interfacess
{
    public class UsersInterfaces : IUsers
    {
        private readonly AppDbContext context;

        public UsersInterfaces(AppDbContext app)
        {
            context = app;
        }   
        //根据姓名和账号查询整个人的信息
        public object GetUsers(string UsersName, string UsersPwd)
        {
            // return context.Users.Where(a => a.UsersName.Contains(UsersName) && a.UsersPwd == UsersPwd).FirstOrDefault();
            var user = (from users in context.Users join role in context.UserRoles on users.RoleID equals role.RoleID
                        join Role_Permission in context.Role_Permission on users.RoleID equals Role_Permission.RoleID
                        join Permissions in context.permissions on Role_Permission.PermissionID equals Permissions.PermissionID
                        join PerState in context.PermissionStates on Permissions.PermissionStateID equals PerState.PermissionStateID
                        where users.UsersName == UsersName && users.UsersPwd == UsersPwd                        
                        select new
                        {
                            users.UsersID,
                            users.Nickname,
                            users.UsersName,
                            users.StateID,
                            role.RoleID,
                            role.RoleName,
                            Permissions.PermissionName,
                            PerState.PermissionStateName
                        }).FirstOrDefault();
            return user;
        
        }
        public int GetLogins(string UsersName, string UsersPwd)
        {
           
            int users = context.Users.Where(a => a.UsersName.Contains(UsersName) && a.UsersPwd==UsersPwd).Count();
            return users;
        }
    }
}
